package com.bestcem.xm.wallet.util.business;

import com.bestcem.xm.wallet.config.properties.SingleOrgProperties;
import com.bestcem.xm.wallet.grpc.client.UserCustomSettingGrpcService;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 获取配置工具类
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/16 15:12
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Component
public class EnvSettingUtil {

    @Value("${app.user.redPaperPlatformCode:}")
    private String redPaperPlatformCode;

    @Value("${app.user.server.protocol:HTTP}")
    private String protocol;

    @Value("${app.user.server.xm_server_host:xm-test.bestcem.com}")
    private String serverHost;

    @Autowired
    private SingleOrgProperties singleOrgProperties;

    @Autowired
    private UserCustomSettingGrpcService userCustomSettingGrpcService;

    /**
     * 默认平台代码
     */
    public static final String DEFAULT_PLATFORM_CODE = "bestcem";

    /**
     * 获取平台代码
     *
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/5/16 15:33
     */
    public String getRedPaperPlatformCode() {
        if (StringUtils.isNotBlank(this.redPaperPlatformCode)) {
            return this.redPaperPlatformCode;
        }
        if (this.singleOrgProperties != null && StringUtils.isNotBlank(singleOrgProperties.getCode())) {
            return singleOrgProperties.getCode();
        }
        return DEFAULT_PLATFORM_CODE;
    }

    /**
     * 获取指定公司的域名
     *
     * @param orgId           租户id
     * @param orgCode         租户code
     * @param requestHost     请求host
     * @param requestProtocol 请求协议
     * @return java.lang.String 公司域名
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/10 14:10
     */
    public String getCorpUrl(String orgId, String orgCode, String requestHost, String requestProtocol) {
        if (StringUtils.isNotBlank(singleOrgProperties.getRealUrl())) {
            return singleOrgProperties.getRealUrl();
        }

        StringBuilder stb = new StringBuilder();
        if (StringUtils.isNotBlank(orgCode)) {
            stb.append(protocol).append("://").append(orgCode).append(".").append(serverHost);
            if (StringUtils.isBlank(orgId)) {
                return stb.toString();
            }
            // 根据org_id查询租户自定义配置信息
            String customSetting = Optional.of(userCustomSettingGrpcService.queryCustomSettingByOrgId(orgId).getDomain()).orElse("");
            if (StringUtils.isNotBlank(customSetting)) {
                stb = new StringBuilder();
                return stb.append(protocol).append("://").append(customSetting).toString();
            }
            return stb.toString();
        }

        if (StringUtils.isNotBlank(requestHost) && StringUtils.isNotBlank(requestProtocol)) {
            stb.append(requestProtocol).append("://").append(requestHost);
        } else {
            stb.append(protocol).append("://").append(serverHost);
        }
        return stb.toString();
    }

}
